package sub

import (
	"fmt"
	"quipus/cmd/qpc/sub"
	"quipus/internal/utils"
	v1pb "quipus/proto/gen/api/v1"

	"github.com/spf13/cobra"
)

var (
	name       string
	modelId    int32
	prompt     string
	promptFile string
)

func init() {
	createCmd.PersistentFlags().StringVarP(&name, "name", "n", "", "unique name for llm model entity")
	createCmd.PersistentFlags().Int32VarP(&modelId, "model", "m", 0, "the model id for the assistant")
	createCmd.PersistentFlags().StringVarP(&prompt, "prompt", "p", "", "the prompt for the assistant")
	createCmd.PersistentFlags().StringVar(&promptFile, "pf", "", "the prompt file path for the assistant. If set this value, ignore prompt parmas")

	assistantCmd.AddCommand(createCmd)
}

var createCmd = &cobra.Command{
	Use:   "create",
	Short: "Run the cmd to create assistant",
	RunE: func(cmd *cobra.Command, args []string) error {
		qconn, e := sub.NewGrpcConnect(sub.QpConfig.QPAddress)
		if e != nil {
			return e
		}

		qclient := v1pb.NewLLMServiceClient(qconn)
		ctxWithToken := sub.NewCtxWithToken(sub.QpConfig.Token)
		model := utils.BuildUrn(utils.ModelUrnPrefix, modelId)
		if promptFile != "" {
			p, e := utils.ReadFileStr(promptFile)
			if e != nil {
				return e
			}
			prompt = p
		}

		in := &v1pb.CreateLLMAssistantRequest{Name: name, Model: model, Prompt: prompt}

		rsp, err := qclient.CreateLLMAssistant(ctxWithToken, in)
		if err != nil {
			return err
		}
		fmt.Print(rsp)
		return nil
	},
}
